package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppGoodsSearchParam;
import com.jnzc.ocr.serv.shop.api.ShopGoodsApi;
import com.jnzc.ocr.serv.shop.entity.ShopGoods;
import com.jnzc.ocr.serv.shop.filter.ShopGoodsFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangcm
 * @version 1.0, 2017-10-05
 */
@Api(tags = {"app - 商品接口"})
@RestController
@RequestMapping("/app/shopGoods")
public class AppGoodsController extends BaseController {

    @Autowired
    private ShopGoodsApi shopGoodsApi;

    @ApiOperation(value = "分页")
    @GetMapping("")
    public PageResult<ShopGoods> search(AppGoodsSearchParam param, Page page){
        UserVo userVo = super.get();
        ShopGoodsFilter filter = new ShopGoodsFilter();
        filter.setShopId(userVo.getShopId());
        filter.setCategoryId(param.getCategoryId());
        filter.setIfOnSale(true);
        filter.setKeyword(param.getKeyword());
        if(param.getSortField() == 2){
            filter.setOrderBy("sale_num");
        }else{
            filter.setOrderBy("update_time");
        }
        if(param.getIfDesc()){
            filter.setOrderBy(filter.getOrderBy() + " desc");
        }
        return shopGoodsApi.search(filter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopGoods info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopGoodsApi.find(id, userVo.getShopId());
    }
}
